﻿#define _CRT_SECURE_NO_WARNINGS 1

#include "List.h"

// 申请结点
LTNode* buyNode()
{
	LTNode* node = (LTNode*)malloc(sizeof(LTNode));
	
	if (node == NULL)
	{
		perror("malloc fail!\n");
		exit(-1);
	}

	node->data = -1;
	node->next = node->prev = node;

	return node;
}

//初始化
//void LTInit(LTNode** pphead)
//{
//	assert(pphead && *pphead);
//
//	(*pphead)->data = -1;
//	(*pphead)->next = (*pphead)->prev = *pphead;
//}

LTNode* LTInit()
{
	LTNode* phead = buyNode();
	return phead;
}

// 打印
void LTPrint(LTNode* phead)
{
	LTNode* pcur = phead->next;	// 指向哨兵位的下一个结点，即第一个有效的结点

	while (pcur != phead)
	{
		printf("%d -> ", pcur->data);
		pcur = pcur->next;
	}
	printf("\n");
}

// 判断是否是空链表
bool LTEmpty(LTNode* phead)
{
	assert(phead);
	return phead->next == phead;	// 只有一个哨兵结点就为空链表
}

// 尾插
void LTPushBack(LTNode* phead, LTDatatype x)
{
	assert(phead);

	LTNode* newnode = buyNode();
	newnode->data = x;
	// phead->prev newnode phead
	newnode->next = phead;
	newnode->prev = phead->prev;

	phead->prev->next = newnode;
	phead->prev = newnode;
}

// 头插
void LTPushFront(LTNode* phead, LTDatatype x)
{
	assert(phead);

	LTNode* newnode = buyNode();
	newnode->data = x;

	// phead newnode phead->next
	newnode->next = phead->next;
	newnode->prev = phead;

	phead->next->prev = newnode;
	phead->next = newnode;
}

//尾删
void LTPopBack(LTNode* phead)
{
	assert(!LTEmpty(phead));	// 删除不能是空链表

	LTNode* del = phead->prev;
	// del->prev del phead
	del->prev->next = phead;
	phead->prev = del->prev;
	free(del);
	del = NULL;
}

// 头删
void LTPopFront(LTNode* phead)
{
	assert(!LTEmpty(phead));	// 删除不能是空链表

	LTNode* del = phead->next;

	// phead del del->next
	phead->next = del->next;
	del->next->prev = phead;
	free(del);
	del = NULL;
}

//在pos位置之后插⼊数据
void LTInsertAfter(LTNode* pos, LTDatatype x)
{
	assert(pos);

	LTNode* newnode = buyNode();
	newnode->data = x;

	// pos newnode pos->next
	newnode->next = pos->next;
	newnode->prev = pos;

	pos->next->prev = newnode;
	pos->next = newnode;
}

//在pos位置之前插⼊数据
void LTInsertBefore(LTNode* pos, LTDatatype x)
{
	assert(pos);

	LTNode* newnode = buyNode();
	newnode->data = x;

	//pos->prev newnode pos
	newnode->next = pos;
	newnode->prev = pos->prev;

	pos->prev->next = newnode;
	pos->prev = newnode;
}

// 删除指定位置的数据
void LTErase(LTNode* pos)
{
	assert(pos);

	// pos->prev pos pos->next
	pos->prev->next = pos->next;
	pos->next->prev = pos->prev;
	free(pos);
	pos = NULL;
}

// 查找
LTNode* LTFind(LTNode* phead, LTDatatype x)
{
	assert(phead);

	LTNode* pcur = phead->next;
	while (pcur != phead)
	{
		if (pcur->data == x)
			return pcur;
		pcur = pcur->next;
	}
	return NULL;
}

//void LTDesTroy(LTNode** pphead)
//{
//	LTNode* pcur = (*pphead)->next;
//	LTNode* next = NULL;
//
//	while (pcur != *pphead)
//	{
//		next = pcur->next;
//		free(pcur);
//		pcur = next;
//	}
//	free(*pphead);
//	*pphead = NULL;
//}
//接口一致性,传一级，在函数调用结束后需要手动将实参置为NULL
void LTDesTroy(LTNode* phead)
{
	LTNode* pcur = phead->next;
	LTNode* next = NULL;
	
	while (pcur != phead)
	{
		next = pcur->next;
		free(pcur);
		pcur = next;
	}
	free(phead);
	phead = NULL;
}